package com.exsun.ex1077.ui.activity.main

import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.alibaba.fastjson.JSONObject
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.target.Target
import com.exsun.ex1077.app.Constant
import com.exsun.ex1077.network.apiService
import com.exsun.ex1077.network.entity.*
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.base.appContext
import com.zhangping.fastjetpack.extend.base.ResultState
import com.zhangping.fastjetpack.extend.base.request
import com.zhangping.fastjetpack.extend.databind.BooleanObservableField
import com.zhangping.fastjetpack.extend.databind.IntObservableField
import com.zhangping.fastjetpack.extend.databind.StringObservableField
import com.zhangping.fastjetpack.extend.putCache
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2022/8/8 10:48
 */
class MainViewModel: BaseViewModel() {

    //当前选中车辆
    val detailModel = ObservableField<MapVehicle>()
    //显示车辆信息面板
    val showVehicleInfo = BooleanObservableField()
    //未读消息数
    val unreadMsgCount = StringObservableField()
    //当前车辆显示类型(0全部，1在线，2离线,3自定义)
    val showVehicleType = IntObservableField()

    val mapVehicleListResult = MutableLiveData<ResultState<MapDataModel?>>()
    val mapVehicleGpsResult = MutableLiveData<ResultState<MapVehicleGps?>>()
    val mapMileageRankResult = MutableLiveData<ResultState<List<MileageRank>>>()
    val objectTypeResult = MutableLiveData<ResultState<List<Int>>>()
    val mapBoundsResult = MutableLiveData<ResultState<List<Bounds>>>()
    val menuPermissionResult = MutableLiveData<ResultState<List<MenuTree>>>()

    fun loadMapData(showLoading: Boolean = false){
        request({ apiService.mapVehicleList()}, mapVehicleListResult, isShowDialog = showLoading)
    }

    fun loadMenuPermission(){
        request({ apiService.menuPermission()}, menuPermissionResult)
    }

    fun loadVehicleData(deviceNo: String){
        request({ apiService.mapVehicleGps(deviceNo)}, mapVehicleGpsResult)
    }

    fun loadUnReadMsgCount(){
        request({ apiService.msgUnReadCount()}, {
            it?.let {
                unreadMsgCount.set(if (it.unReadCount > 99) "99+" else (if (it.unReadCount <= 0) "" else it.unReadCount.toString()))
            }
        })
    }

    fun loadMileageRank(){
        request({ apiService.mapDeviceMileageRank()}, mapMileageRankResult)
    }

    fun loadCheckObject(){
        request({ apiService.checkObject()}, objectTypeResult)
    }

    fun loadBoundsData(){
        request({ apiService.mapBoundsList(JSONObject().apply {
            put("types", arrayListOf(
                Constant.BOUNDS_TYPE_POLYGON,
                Constant.BOUNDS_TYPE_LINE
            ))
        })}, mapBoundsResult)
    }

    fun loadVideoCloseTime(){
        request({ apiService.videoCloseTime()}, {
            "liveCloseTime".putCache(it)
        })
    }
}